<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;
use app\common\service\GuguService;
use app\common\model\School as SchoolModel;


class ChooseSubject extends App
{
    
    //模糊搜索院校列表
    public function get_school()
    {
        
        $kw = $this->request->get("kw",'','trim'); 
        if(empty($kw)){
            return $this->show(2001,"请输入关键词!", '');
        }
		
		
		$map['college_name'] = ['like',"%".$kw."%"];
        $list = Db::name("school")->field('id,college_name')->where($map)->select();
		
		return $this->show(1,"success", $list);
		
    }
    
    //选考科目查询-院校优先
    public function school()
    {
		//$start_time = microtime(true);//计时
		$xk_province_id = $this->xk_province_id;
		$user_id = $this->user_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
        $pagesize = 10;
        $page = $this->request->get("page",0,'intval'); 
        
        $province_id= $this->request->get("province_id",'','trim');
        $school_type= $this->request->get("school_type",'','trim');
        $school_tese= $this->request->get("school_tese",'','trim');
        $school_xingzhi= $this->request->get("school_xingzhi",'','trim'); 
        $school_ids_1= $this->request->get("school_ids",'','trim'); 
		
        if(!empty($province_id)){
			$map['province_id'] = ['in',$province_id];
        }
		$school_list = Db::name("school")->field('school_uuid,id,school_suoshu,school_type,province')->where('status',1)->select();
		if(!empty($school_type)){
			$school_id1 = Db::name('school')->where('school_type','in',$school_type)->column('id');
        }else{
			$school_id1 = array_column($school_list,'id');
		}
        if(!empty($school_tese)){
			$school_id2 = Db::name('school_tese_data')->where('tese_id','in',$school_tese)->column('school_id');
        }else{
			$school_id2 = array_column($school_list,'id');
		}
        if(!empty($school_xingzhi)){
			$school_id3 = Db::name('school')->where('school_xingzhi','in',$school_xingzhi)->column('id');
        }else{
			$school_id3 = array_column($school_list,'id');
		}
		$school_ids = array_intersect($school_id1,$school_id2,$school_id3);
		$map['id'] = ['in',$school_ids];
		//$map['status'] = 1; 
		$school_uuids_ed = Db::name("school")->where($map)->column('school_uuid');
		$year = date('Y',strtotime("-1 year"));
		$map1['year'] = $year;
		$map1['province_id'] = $xk_province_id;
		$map1['school_uuid'] = ['in',$school_uuids_ed];
        $count = Db::name("school_line_new")->where($map1)->group('college_name')->count();
		//echo Db::name("school_line_new")->getlastsql();
		$list = Db::name("school_line_new")->field('cover_image,college_name,school_uuid,school_type as school_xingzhi')->where($map1)->group('college_name')->order('id asc')
			->page($page,$pagesize)->select(); 
        if(!empty($school_ids_1)){
			$school_uuids_ed_1 = Db::name("school")->where('id','in',$school_ids_1)->where('status',1)->column('school_uuid');
			$list1 = Db::name("school_line_new")->field('cover_image,college_name,school_uuid,school_type as school_xingzhi')->where('school_uuid','in',$school_uuids_ed_1)->where('year',$year)->where('province_id',$xk_province_id)->group('college_name')->order('id asc')->select();  
			if(!empty($list1)){
				foreach($list1 as $ks=>$vs){
					array_unshift($list,$vs);
				}
			}
        }
		//整理好所有大学的数据
		$school_id_ed = array_column($school_list,'id','school_uuid');
		$school_province_ed = array_column($school_list,'province','school_uuid');
		$school_type_ed = array_column($school_list,'school_type','school_uuid');
		$school_tese = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id','in',$school_ids)->field('d.school_id,t.title')->select();
		$school_tese_ed = [];
		foreach($school_tese as $kt=>$vt){
			$school_tese_ed[$vt['school_id']][] = $vt['title'];
		}
		$school_type_arr = Db::name('school_type')->column('id,title');
		
        //$subject_selection_arr = [];
		//$subject_selection_list = Db::name("subject_selection")->select();
		//foreach($subject_selection_list as $k=>$v){
		//	$subject_selection_arr[$v['title']] = $v;
		//}
		//dump($list);die;
		$subject_mode = SchoolModel::getSubjectMode($xk_province_id);
		$subject_mode_title = Db::name("member")->where('id',$user_id)->value('cj_subject_mode_title');		
		$major_subject_list = SchoolModel::getMajorSubjectList();	
		//	dump($major_subject_list);
		//$end_time = microtime(true);//计时
		//echo "执行时间: " . ($end_time - $start_time) . " 秒";die;
		
		$school_uuids_major = array_column($list,'school_uuid');
		$major_nums = Db::name("major_line_new")->field("school_name,school_uuid,count(*) as nums")->where('year',$year)->where('province_id',$xk_province_id)->where('school_uuid','in',$school_uuids_major)->group('school_name')->select(); 
		$major_nums = array_column($major_nums,'nums','school_uuid');
		
		$zuhe_list = Db::name("major_line_new")->alias('m')->join('subject_selection s','m.subject_selection=s.title')->field("school_uuid,sum(wu) as wu,sum(hua) as hua,sum(sheng) as sheng,sum(zheng) as zheng,sum(shi) as shi,sum(di) as di")->where('m.school_uuid','in',$school_uuids_major)->where('m.year',$year)->where('province_id',$xk_province_id)->group('school_name')->select();
		$zuhe_list_new = [];
		foreach($zuhe_list as $ks=>$major_subject){
			$zuhe = '';
			$school_uuid = $major_subject['school_uuid'];
			unset($major_subject['school_uuid']);
			if(!empty($major_subject)){
				arsort($major_subject);
				if($subject_mode == 2){
					$major_subject = array_keys(array_splice($major_subject,0,3));
				}else{
					unset($major_subject['wu']);
					unset($major_subject['shi']);
					$major_subject = array_keys(array_splice($major_subject,0,2));
					if($subject_mode_title == '历史类'){
						array_unshift($major_subject,'shi');
					}else{
						array_unshift($major_subject,'wu');
					}
				}
				foreach($major_subject as $ks1=>$vs1){
					$major_subject[$ks1] = !empty($major_subject_list[$vs1])?$major_subject_list[$vs1]:'';
				}
				$zuhe = implode('+',$major_subject);
			}
			$zuhe_list_new[$school_uuid] = $zuhe;
		}
		
		foreach($list as $k=>$v){
			$school_id = !empty($school_id_ed[$v['school_uuid']])?$school_id_ed[$v['school_uuid']]:'';
			$list[$k]['province'] = !empty($school_province_ed[$v['school_uuid']])?$school_province_ed[$v['school_uuid']]:'';
			$school_type = !empty($school_type_ed[$v['school_uuid']])?$school_type_ed[$v['school_uuid']]:'';
			$list[$k]['school_type'] = !empty($school_type_arr[$school_type])?$school_type_arr[$school_type]:'';
			$list[$k]['school_tese'] = !empty($school_tese_ed[$school_id])?$school_tese_ed[$school_id]:'';
			//echo Db::name("major_line_new")->getlastsql();die;
			
			/*
			$list[$k]['major_nums'] = Db::name("major_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('school_uuid',$v['school_uuids_major'])->count();
			$zuhe = '';
			$major_subject = Db::name("major_line_new")->alias('m')->join('subject_selection s','m.subject_selection=s.title')->field("sum(wu) as wu,sum(hua) as hua,sum(sheng) as sheng,sum(zheng) as zheng,sum(shi) as shi,sum(di) as di")->where('m.school_name',$v['college_name'])->where('m.year',$year)->find();
			if(!empty($major_subject)){
				arsort($major_subject);
				if($subject_mode == 2){
					$major_subject = array_keys(array_splice($major_subject,0,3));
					foreach($major_subject as $km=>$vm){
						$major_subject[$km] = !empty($major_subject_list[$vm])?$major_subject_list[$vm]:'';
					}
					$zuhe = implode('+',$major_subject);
				}else{
					$major_subject = array_filter($major_subject, function($value) {
						return $value !== 'wu' && $value !== 'li';
					});
					$major_subject = array_keys(array_splice($major_subject,0,2));
					if($subject_mode_title == '历史类'){
						array_unshift($major_subject,'历史');
					}else{
						array_unshift($major_subject,'物理');
					}
					$zuhe = implode('+',$major_subject);
				}
			} 
			$list[$k]['zuhe'] = $zuhe;*/
			$list[$k]['major_nums'] = !empty($major_nums[$v['school_uuid']])?$major_nums[$v['school_uuid']]:'';
			$list[$k]['zuhe'] = !empty($zuhe_list_new[$v['school_uuid']])?$zuhe_list_new[$v['school_uuid']]:'';
			
		}
		
		//$end_time = microtime(true);//计时
		//echo "执行时间: " . ($end_time - $start_time) . " 秒";die;	
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		//dump($cate_list);die;
		return $this->show(1,"成功", $data);
		
    }
	
	public function get_school_major()
    {
        $pagesize = 10;
        $page = $this->request->get("page",1,'intval');
		
		$user_id = $this->user_id;
        $school_id = $this->request->get("school_id",0,'intval'); 
        if(empty($school_id)){
            return $this->show(2001,"缺少参数school_id!", '');
        }
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
		
		$year = date('Y',strtotime("-1 year"));
		
		
		$school_uuid = Db::name("school")->where('id',$school_id)->value('school_uuid');
		$count = Db::name("major_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('school_uuid',$school_uuid)->count();
		$list = Db::name("major_line_new")->field('school_name,major_name,subject_selection,batch_name')->where('year',$year)->where('province_id',$xk_province_id)->where('school_uuid',$school_uuid)->order('id asc')->page($page,$pagesize)->select();
		
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		//dump($cate_list);die;
		return $this->show(1,"成功", $data);
		
    }
	
	
	
	
	public function get_major_cate_one_list()
    {
        
       
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$education_level = $education_level_list[0];
		$cate_list = Db::name("major")->where('cate_level',1)->where('education_level',$education_level)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
	
		return $this->show(1,"success", $cate_list);
		
    }
    
	public function get_major_cate_two_list()
    {
        
        $cate_id= $this->request->get("cate_id",0,'intval'); 
        if(empty($cate_id)){
            return $this->show(2001,"缺少参数cate_id!", '');
        }
		
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$education_level = $education_level_list[0];
		
		$cate_list = Db::name("major")->where('cate_level',2)->where('education_level',$education_level)->where('cate_pid',$cate_id)->field('id,cate_title,major_code')->order('sort asc,id asc')->select();
		
		return $this->show(1,"success", $cate_list);
		
    }
	
	//模糊搜索专业列表
    public function get_major()
    {
        
        $kw = $this->request->get("kw",'','trim'); 
        if(empty($kw)){
            return $this->show(2001,"请输入关键词!", '');
        }
        $education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$education_level = $education_level_list[0];
		
		
		$map['cate_title'] = ['like',"%".$kw."%"];
        $list = Db::name("major")->field('id,cate_title as major_name')->where('education_level',$education_level)->where('cate_level',3)->where($map)->select();
		
		return $this->show(1,"success", $list);
		
    }
	//选考科目查询-专业优先
	public function major()
    {
        $pagesize = 10;
        $page = $this->request->get("page",1,'intval');
		
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
		
        $cate_id = $this->request->get("cate_id",0,'intval'); 
		$majorname = '';
        if(!empty($cate_id)){
			$majorname = Db::name("major")->where('id',$cate_id)->value('cate_title');
        }
		$year = date('Y',strtotime("-1 year"));
		
		
		$count = Db::name("major_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('major_name',$majorname)->count();
		$list = Db::name("major_line_new")->field('school_name,major_name,subject_selection,batch_name')->where('year',$year)->where('province_id',$xk_province_id)->where('major_name',$majorname)->order('id asc')->page($page,$pagesize)->select();
		
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		return $this->show(1,"success", $data);
		
    }
	
	
	//智能选科和自主选科-院校优先
    public function intelligence_school()
    {
        $pagesize = 10;
        $page = $this->request->get("page",0,'intval'); 
		$year = date('Y',strtotime("-1 year"));
		$user_id = $this->user_id;
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
		
        
        $province_id= $this->request->get("province_id",'','trim');
        $school_type= $this->request->get("school_type",'','trim');
        $school_tese= $this->request->get("school_tese",'','trim');
        $school_xingzhi= $this->request->get("school_xingzhi",'','trim'); 
        $school_ids_1= $this->request->get("school_ids",'','trim'); 
        if(!empty($province_id)){
			$map['province_id'] = ['in',$province_id];
        }
		
        if(!empty($school_type)){
			//$school_id1 = Db::name('school_type_data')->where('type_id','in',$school_type)->column('school_id');
			$school_id1 = Db::name('school')->where('school_type','in',$school_type)->column('id');
			$map['id'] = ['in',$school_id1];
        }
        if(!empty($school_tese)){
			$school_id2 = Db::name('school_tese_data')->where('tese_id','in',$school_tese)->column('school_id');
			$map['id'] = ['in',$school_id2];
        }
        if(!empty($school_xingzhi)){
			//$school_id3 = Db::name('school_xingzhi_data')->where('xingzhi_id','in',$school_xingzhi)->column('school_id');
			$school_id3 = Db::name('school')->where('school_xingzhi','in',$school_xingzhi)->column('id');
			$map['id'] = ['in',$school_id3];
        }
		if(isset($school_id1) && isset($school_id2) && isset($school_id3)){
			$school_ids = array_intersect($school_id1,$school_id2,$school_id3);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id1) && isset($school_id2)){
			$school_ids = array_intersect($school_id1,$school_id2);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id1) && isset($school_id3)){
			$school_ids = array_intersect($school_id1,$school_id3);
			$map['id'] = ['in',$school_ids];
		}elseif(isset($school_id2) && isset($school_id3)){
			$school_ids = array_intersect($school_id2,$school_id3);
			$map['id'] = ['in',$school_ids];
		}
		
        
		//dump(implode(',', array_fill(0, count($school_xingzhi), '%')));die;
		$map['status'] = 1;
        $count = Db::name("school")->where($map)->count();
		$list = Db::name("school")->where($map)->field('id,college_name,province_id,ranking,cover_image,school_type,school_xingzhi,school_uuid')->order('ranking asc,id desc')
			->page($page,$pagesize)->select();  
		
        if(!empty($school_ids_1)){
			$list1 = Db::name("school")->where('id','in',$school_ids_1)->field('id,college_name,province_id,ranking,cover_image,school_type,school_xingzhi,school_uuid')->order('ranking asc,id desc')->select();  
			//$list = array_merge($list,$list1);
			if(!empty($list1)){
				$school_ids_ed = Db::name("school")->where($map)->column('id');
				foreach($list1 as $ks=>$vs){
					if(!in_array($vs['id'],$school_ids_ed)){
						array_unshift($list,$vs);
					}
				}
				
			}
        }
		
		//整理好最优组合和专业数量
		$subject_mode = SchoolModel::getSubjectMode($xk_province_id);
		$subject_mode_title = Db::name("member")->where('id',$user_id)->value('cj_subject_mode_title');		
		$major_subject_list = SchoolModel::getMajorSubjectList();	
		
		$school_uuids_major = array_column($list,'school_uuid');
		$major_nums = Db::name("major_line_new")->field("school_name,school_uuid,count(*) as nums")->where('year',$year)->where('province_id',$xk_province_id)->where('school_uuid','in',$school_uuids_major)->group('school_name')->select(); 
		$major_nums = array_column($major_nums,'nums','school_uuid');
		
		$zuhe_list = Db::name("major_line_new")->alias('m')->join('subject_selection s','m.subject_selection=s.title')->field("school_uuid,sum(wu) as wu,sum(hua) as hua,sum(sheng) as sheng,sum(zheng) as zheng,sum(shi) as shi,sum(di) as di")->where('m.school_uuid','in',$school_uuids_major)->where('m.year',$year)->where('province_id',$xk_province_id)->group('school_name')->select();
		$zuhe_list_new = [];
		foreach($zuhe_list as $ks=>$major_subject){
			$zuhe = '';
			$school_uuid = $major_subject['school_uuid'];
			unset($major_subject['school_uuid']);
			if(!empty($major_subject)){
				arsort($major_subject);
				if($subject_mode == 2){
					$major_subject = array_keys(array_splice($major_subject,0,3));
				}else{
					unset($major_subject['wu']);
					unset($major_subject['shi']);
					$major_subject = array_keys(array_splice($major_subject,0,2));
					if($subject_mode_title == '历史类'){
						array_unshift($major_subject,'shi');
					}else{
						array_unshift($major_subject,'wu');
					}
				}
				foreach($major_subject as $ks1=>$vs1){
					$major_subject[$ks1] = !empty($major_subject_list[$vs1])?$major_subject_list[$vs1]:'';
				}
				$zuhe = implode('+',$major_subject);
			}
			$zuhe_list_new[$school_uuid] = $zuhe;
		}
		
		//dump($zuhe_list);dump($zuhe_list_new);die;
		foreach($list as $k=>$v){
			$list[$k]['province'] = Db::name('area')->where('id',$v['province_id'])->value('name');
			$list[$k]['school_type'] = Db::name('school_type')->where('id',$v['school_type'])->value('title');
			$list[$k]['school_xingzhi'] = Db::name('school_xingzhi')->where('id',$v['school_xingzhi'])->value('title');
			$list[$k]['school_tese'] = Db::name('school_tese_data')->alias('d')->join('school_tese t','d.tese_id=t.id')->where('d.school_id',$v['id'])->column('t.title');
			$list[$k]['major_nums'] = !empty($major_nums[$v['school_uuid']])?$major_nums[$v['school_uuid']]:'';
			$list[$k]['zuhe'] = !empty($zuhe_list_new[$v['school_uuid']])?$zuhe_list_new[$v['school_uuid']]:'';
		}
		
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		//dump($cate_list);die;
		return $this->show(1,"成功", $data);
		
    }
	//智能选科-专业优先
	public function intelligence_major()
    {
		$user_id = $this->user_id;
        $pagesize = 10;
        $page = $this->request->get("page",1,'intval');
		$year = date('Y',strtotime("-1 year"));
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
		
        $major_ids = $this->request->get("major_ids",'','trim');
		
        $cate_ids = $this->request->get("cate_ids",'','trim'); 
        if(!empty($cate_ids)){
			$map['id'] = ['in',$cate_ids];
        }
		$map['status'] = 1;
		$map['cate_level'] = 2;
		$education_level_list = Db::name("major")->group('education_level')->column('education_level');
        if(empty($education_level_list)){
            return $this->show(2001,"没有数据!", '');
        }
		$map['education_level'] = $education_level_list[0];
		
        $count = Db::name("major")->where($map)->count();
        $pages = ceil($count/$pagesize);
		$list = Db::name("major")->where($map)->field('id,disciplinary_category,cate_title,major_code')->order('major_code asc,id desc')
			->page($page,$pagesize)->select();  
		
		if(!empty($major_ids)){
			$cate_pids = Db::name("major")->where('cate_level',3)->where('education_level',$education_level_list[0])->where('id','in',$major_ids)->column('cate_pid');  
			$cate_pids = array_unique($cate_pids);//dump($cate_pids);die;
			$list1 = Db::name("major")->where('id','in',$cate_pids)->field('id,disciplinary_category,cate_title,major_code')->select();
			if(!empty($list1)){
				$major_ids_ed = Db::name("major")->where($map)->column('id');
				foreach($list1 as $ks=>$vs){
					if(!in_array($vs['id'],$major_ids_ed)){
						array_unshift($list,$vs);
					}
				}
				
			}
        }
		
		//dump($list);
		//整理好最优组合和专业数量
		$subject_mode = SchoolModel::getSubjectMode($xk_province_id);
		$subject_mode_title = Db::name("member")->where('id',$user_id)->value('cj_subject_mode_title');		
		$major_subject_list = SchoolModel::getMajorSubjectList();
		$zuhe_list = [];
		foreach($list as $k=>$v){
			$zuhe_major_codes = Db::name("major")->where('cate_level',3)->where('cate_pid',$v['id'])->column('major_code');  
			$zuhe_list[$v['major_code']] = Db::name("major_line_new")->alias('m')->join('subject_selection s','m.subject_selection=s.title')->field("sum(wu) as wu,sum(hua) as hua,sum(sheng) as sheng,sum(zheng) as zheng,sum(shi) as shi,sum(di) as di")->where('m.major_standard_code','in',$zuhe_major_codes)->where('m.year',$year)->where('m.province_id',$xk_province_id)->find();
		}
		
		$zuhe_list_new = [];
		//dump($zuhe_list);die;
		foreach($zuhe_list as $ks=>$major_subject){
			$zuhe = '';
			if(!empty($major_subject)){
				arsort($major_subject);
				if($subject_mode == 2){
					$major_subject = array_keys(array_splice($major_subject,0,3));
				}else{
					unset($major_subject['wu']);
					unset($major_subject['shi']);
					$major_subject = array_keys(array_splice($major_subject,0,2));
					if($subject_mode_title == '历史类'){
						array_unshift($major_subject,'shi');
					}else{
						array_unshift($major_subject,'wu');
					}
				}
				foreach($major_subject as $ks1=>$vs1){
					$major_subject[$ks1] = !empty($major_subject_list[$vs1])?$major_subject_list[$vs1]:'';
				}
				$zuhe = implode('+',$major_subject);
			}
			$zuhe_list_new[$ks] = $zuhe;
		}
		//dump($zuhe_list_new);
		foreach($list as $k=>$v){
			$list[$k]['zuhe'] = !empty($zuhe_list_new[$v['major_code']])?$zuhe_list_new[$v['major_code']]:'';
			$list[$k]['major_nums'] = Db::name("major")->where('cate_level',3)->where('cate_pid',$v['id'])->count();
			$list[$k]['school_nums'] = '';
		}
		
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		return $this->show(1,"success", $data);
		
    }
	
	//智能选科和自主选科-专业优先-匹配院校
	public function get_intelligence_major_school()
    {
        $pagesize = 10;
        $page = $this->request->get("page",1,'intval');
		
		$user_id = $this->user_id;
        $cate_id = $this->request->get("cate_id",0,'intval'); 
        if(empty($cate_id)){
            return $this->show(2001,"缺少参数cate_id!", '');
        }
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
		
		$year = date('Y',strtotime("-1 year"));
		
		$major_codes = Db::name("major")->where('cate_level',3)->where('cate_pid',$cate_id)->column('major_code');  
		
		
		$count = Db::name("major_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('major_standard_code','in',$major_codes)->count();
		//dump($major_codes);die;
		$list = Db::name("major_line_new")->field('school_name,major_name,subject_selection,batch_name,type_name')->where('year',$year)->where('province_id',$xk_province_id)->where('major_standard_code','in',$major_codes)->order('id asc')->page($page,$pagesize)->select();
		
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);	
		//dump($cate_list);die;
		return $this->show(1,"成功", $data);
		
    }
	
	
	//智能选科和自主选科-获取科目列表
    public function get_subjects()
    {
		$province_id = $this->xk_province_id;
        if(empty($province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
		
		$subject_mode = SchoolModel::getSubjectMode($province_id);	
		if($subject_mode == 2){
			$data['subjects1'] = ['物理','化学','生物','历史','地理','政治'];
			if($province_id == 933){
				$data['subjects1'] = ['物理','化学','生物','历史','地理','政治','技术'];
			}elseif($province_id == 801){
				$data['subjects1'] = ['物理','化学','生物','历史','地理','政治','科学'];
			}
			$data['subject_mode'] = 2;
		}else{
			$data['subjects1'] = ['物理','历史'];
			$data['subjects2'] = ['化学','生物','地理','政治'];
			$data['subject_mode'] = 1;
		}
		return $this->show(1,"success", $data);
		
    }
	
	//智能选科和自主选科-院校匹配率
	public function get_school_match_rate()
    {
		
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
        $subjects = $this->request->get("subjects"); 
        if(empty($subjects)){
            return $this->show(2001,"请选择科目!", '');
        }
		$subjects = explode(',',$subjects);
		if(empty($subjects) || count(array_unique($subjects)) != 3){
			return $this->show(2001,"请选择正确科目!", '');
		}
		
		$subject_mode = SchoolModel::getSubjectMode($xk_province_id);	
		if($subject_mode == 2){
			$subject_mode_title = '综合';
		}else{
			if($subjects[0] == '物理'){
				$subject_mode_title = '物理类';
			}elseif($subjects[0] == '历史'){
				$subject_mode_title = '历史类';
			}else{
				return $this->show(2001,"请选择正确科目1!", '');
			}
		}
		
		$subject_selection = Db::name("subject_selection")->where('subject_mode_title',$subject_mode_title)->select();
		$subject_selection_new = [];
		foreach($subject_selection as $k=>$v){
			if($v['type'] == 0){
				$subject_selection_new[] = $v['title'];
			}elseif($v['type'] == 1){//多选一
				$temp = SchoolModel::getXuyaoSubject($v,$subject_mode_title);	 
				foreach($temp as $k1=>$v1){
					if(in_array($v1,$subjects)){
						$subject_selection_new[] = $v['title'];
						break;
					}	 
				}
			}elseif($v['type'] == 2){//必选
				$temp = SchoolModel::getXuyaoSubject($v,$subject_mode_title);	 
				$flag = 1;
				foreach($temp as $k1=>$v1){
					if(!in_array($v1,$subjects)){
						$flag = 0;
						break;
					}	 
				}
				if($flag == 1){
					$subject_selection_new[] = $v['title'];
				}
			}else{
				$subject_selection_new[] = $v['title'];
			}
		}
		
		array_push($subject_selection_new,'');
		$year = date('Y',strtotime("-1 year"));
		$total_nums = Db::name("school_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('type_name',$subject_mode_title)->group('college_name')->count();
		$match_nums = Db::name("school_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('type_name',$subject_mode_title)->group('college_name')->where('course_selection','in',$subject_selection_new)->count();
		
		$match_rate = ($total_nums == 0)?0:ceil(100*$match_nums/$total_nums);
		$match_rate_title = ($match_rate>=50)?'高':'低';
		//dump($total_nums);dump($match_nums);
		
		//最优组合
		$zuhe_zuiyou = '';
		$major_subject_list = SchoolModel::getMajorSubjectList();
		$major_subject = Db::name("major_line_new")->alias('m')->join('subject_selection s','m.subject_selection=s.title')->field("sum(wu) as wu,sum(hua) as hua,sum(sheng) as sheng,sum(zheng) as zheng,sum(shi) as shi,sum(di) as di")->where('m.year',$year)->where('province_id',$xk_province_id)->where('type_name',$subject_mode_title)->find();
		arsort($major_subject);
		if($subject_mode == 2){
			$major_subject = array_keys(array_splice($major_subject,0,3));
		}else{
			unset($major_subject['wu']);
			unset($major_subject['shi']);
			$major_subject = array_keys(array_splice($major_subject,0,2));
			if($subject_mode_title == '历史类'){
				array_unshift($major_subject,'shi');
			}else{
				array_unshift($major_subject,'wu');
			}
		}
		foreach($major_subject as $ks1=>$vs1){
			$major_subject[$ks1] = !empty($major_subject_list[$vs1])?$major_subject_list[$vs1]:'';
		}
		$zuhe_zuiyou = implode('+',$major_subject);
		
		
		return $this->show(1,"success", ['total_nums'=>$total_nums,'match_nums'=>$match_nums,'match_rate'=>$match_rate.'%','match_rate_title'=>$match_rate_title,'zuhe_zuiyou'=>$zuhe_zuiyou]);
		
	}
    //智能选科和自主选科-专业匹配率
	public function get_major_match_rate()
    {
		
		$xk_province_id = $this->xk_province_id;
        if(empty($xk_province_id)){
            return $this->show(2001,"请选择选科省份!", '');
        }
        $subjects = $this->request->get("subjects"); 
        if(empty($subjects)){
            return $this->show(2001,"请选择科目!", '');
        }
		$subjects = explode(',',$subjects);
		if(empty($subjects) || count(array_unique($subjects)) != 3){
			return $this->show(2001,"请选择正确科目!", '');
		}
		
		$subject_mode = SchoolModel::getSubjectMode($xk_province_id);	
		if($subject_mode == 2){
			$subject_mode_title = '综合';
		}else{
			if($subjects[0] == '物理'){
				$subject_mode_title = '物理类';
			}elseif($subjects[0] == '历史'){
				$subject_mode_title = '历史类';
			}else{
				return $this->show(2001,"请选择正确科目1!", '');
			}
		}
		
		$subject_selection = Db::name("subject_selection")->where('subject_mode_title',$subject_mode_title)->select();
		$subject_selection_new = [];
		foreach($subject_selection as $k=>$v){
			if($v['type'] == 0){
				$subject_selection_new[] = $v['title'];
			}elseif($v['type'] == 1){//多选一
				$temp = SchoolModel::getXuyaoSubject($v,$subject_mode_title);	 
				foreach($temp as $k1=>$v1){
					if(in_array($v1,$subjects)){
						$subject_selection_new[] = $v['title'];
						break;
					}	 
				}
			}elseif($v['type'] == 2){//必选
				$temp = SchoolModel::getXuyaoSubject($v,$subject_mode_title);	 
				$flag = 1;
				foreach($temp as $k1=>$v1){
					if(!in_array($v1,$subjects)){
						$flag = 0;
						break;
					}	 
				}
				if($flag == 1){
					$subject_selection_new[] = $v['title'];
				}
			}else{
				$subject_selection_new[] = $v['title'];
			}
		}
		
		array_push($subject_selection_new,'');
		$year = date('Y',strtotime("-1 year"));
		$total_nums = Db::name("major_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('type_name',$subject_mode_title)->group('major_name')->count();
		$match_nums = Db::name("major_line_new")->where('year',$year)->where('province_id',$xk_province_id)->where('type_name',$subject_mode_title)->group('major_name')->where('subject_selection','in',$subject_selection_new)->count();
		
		$match_rate = ($total_nums == 0)?0:ceil(100*$match_nums/$total_nums);
		//dump($total_nums);dump($match_nums);
		
		
		return $this->show(1,"success", ['total_nums'=>$total_nums,'match_nums'=>$match_nums,'match_rate'=>$match_rate.'%']);
		
	}
	
    //保存方案
    public function save_subject(){
        $user_id = $this->user_id;
        $province_id = $this->province_id;
        if($this->request->isPost()){
			
			$data['type'] = $this->request->post("type",1,'intval');
			if(!in_array($data['type'],[1,2])){
				return $this->show(2001,"type参数不正确!", '');
			}
			$data['school_ids'] = $this->request->post("school_ids",'','trim');
			$data['major_cate_ids'] = $this->request->post("major_cate_ids",'','trim');
			$data['school_subject_zuhe'] = $this->request->post("school_subject_zuhe",'','trim');
			$data['major_subject_zuhe'] = $this->request->post("major_subject_zuhe",'','trim');
			
			if(empty($data['school_ids']) && empty($data['major_cate_ids'])){
				return $this->show(2001,"请选择院校或专业!", '');
			}
			if(!empty($data['school_ids']) && empty($data['school_subject_zuhe'])){
				return $this->show(2001,"缺少参数school_subject_zuhe!", '');
			}
			if(!empty($data['major_cate_ids']) && empty($data['major_subject_zuhe'])){
				return $this->show(2001,"缺少参数major_subject_zuhe!", '');
			}
			$data['create_time'] = time();
			$data['user_id'] = $user_id;
			$data['province_id'] = $province_id;
			$title = Db::name("subject_fangan")->where('user_id',$user_id)->order('id desc')->value('title');
			$data['title'] = $title+1;
			$res = Db::name("subject_fangan")->insert($data);
			if( $res !== false ){
				return $this->show(1,"success", '');
			}else{
				return $this->show(2002, "fail", "");
			} 
		}else{
            return $this->show(2001, "非法请求", "");
        }
    }
}